package com.culture.config.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.culture.config.common.model.ResponseData;
import com.culture.config.common.util.ResponseUtil;
import com.culture.config.service.*;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping("/api/config")
public class PropertyController extends BaseController{

	@Autowired	
	private PropertyService applicationService;	
	@Autowired	
	private InstanceService instanceService;	
	
	@ApiOperation("page applicaion")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/applicaion/page")
	public ResponseData<List<ApplicaionVO>> pageApplicaions(@RequestBody @Valid ApplicationPageParam param) {
		this.authenticationValidate();
		return ResponseUtil.successPage(this.applicationService.pageApplicaions(param));
	}

	@ApiOperation("list applicaion")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/applicaion/list")
	public ResponseData<List<ApplicaionVO>> listApplicaions(@RequestBody @Valid ApplicationListParam param) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.listApplicaions(param));
	}
	
	@ApiOperation("add applicaion")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/applicaion/add")
	public ResponseData<ApplicaionVO> addApplicaion(@RequestBody @Valid ApplicationAddPO applicationAddPO) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.addApplicaion(applicationAddPO));
	}

	@ApiOperation("copy applicaion")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/applicaion/copy")
	public ResponseData<ApplicaionVO> copyApplicaion(@RequestBody @Valid ApplicationCopyPO copyPO) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.copyApp(copyPO));
	}
	
	@ApiOperation("update applicaion")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/applicaion/update")
	public ResponseData<ApplicaionVO> updateApplicaion(@RequestBody @Valid ApplicationUpdatePO applicationUpdatePO) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.updateApplicaion(applicationUpdatePO));
	}
	
	@ApiOperation("page property")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/property/page")
	public ResponseData<List<PropertyVO>> pageProperties(@RequestBody @Valid PropertyPageParam param) {
		this.authenticationValidate();
		return ResponseUtil.successPage(this.applicationService.pageByApp(param));
	}

	@ApiOperation("list property")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/property/list")
	public ResponseData<List<PropertyVO>> listProperties(@RequestBody @Valid PropertiesParam param) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.getByApp(param));
	}

	@ApiOperation("get properties")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/property/yaml")
	public ResponseData<String> getYaml(@RequestBody @Valid PropertiesParam param) {
		this.authenticationValidate();
		return ResponseUtil.success(this.applicationService.getYamlByApp(param));
	}
	
	@ApiOperation("update property")
	@PostMapping("/property/update")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	public ResponseData<String> updateProperty(@RequestBody @Valid PropertyUpdateParam property) {
		this.authenticationValidate();
		this.applicationService.update(property);
		return ResponseUtil.success();
	}

	@ApiOperation("update property")
	@PostMapping("/property/yaml/update")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	public ResponseData<String> updateProperties(@RequestBody @Valid PropertiesUpdateParam properties) {
		this.authenticationValidate();
		this.applicationService.updateProperties(properties);
		return ResponseUtil.success();
	}
	
	@ApiOperation("add property")
	@PostMapping("/property/add")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	public ResponseData<String> addProperty(@RequestBody @Valid PropertyAddParam property) {
		this.authenticationValidate();
		this.applicationService.add(property);
		return ResponseUtil.success();
	}

	@ApiOperation("sync property")
	@PostMapping("/property/sync")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	public ResponseData<String> syncProperty(@RequestBody @Valid PropertySyncPO syncPO) {
		this.authenticationValidate();
		this.applicationService.syncProperty(syncPO);
		return ResponseUtil.success();
	}

	@ApiOperation("refresh property")
	@PostMapping("/property/refresh")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	public ResponseData<String> refreshProperty(@RequestBody @Valid PropertyRefreshPO refreshPO) {
		this.authenticationValidate();
		this.instanceService.refreshConfiguration(refreshPO);
		return ResponseUtil.success();
	}
	
}
